package com.songshuai.webapi.permission.service.impl;

import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.permission.common.enumerations.DepartmentLevelTypeEnum;
import com.songshuai.webapi.permission.dao.YxDepartmentsMapper;
import com.songshuai.webapi.permission.dto.po.CampusListPojo;
import com.songshuai.webapi.permission.dto.request.RolesReqDto;
import com.songshuai.webapi.permission.dto.response.CampusResDto;
import com.songshuai.webapi.permission.service.CampusService;
import com.songshuai.webapi.permission.service.CurrentUserService;
import com.songshuai.webapi.permission.service.RolesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Set;


/**
 * @Author: hst
 * @Date: 2018/11/23 19:02
 * @Version 1.0
 */
@Slf4j
@Service
public class CampusServiceImpl implements CampusService {

    @Autowired
    private YxDepartmentsMapper departmentsMapper;

    @Autowired
    private CurrentUserService currentUserService;

    @Autowired
    private RolesService rolesService;

    /**
     * 查询当前登录用户所管理的校区
     * @param
     * @return
     */
    @Override
    public QueryResponse<List<CampusResDto>> queryCampusList() {
        Integer currentUserID = currentUserService.getUserId();
        CampusListPojo campusListPojo=new CampusListPojo();
        campusListPojo.setCampusLevelType(DepartmentLevelTypeEnum.Campus.getCode());
        campusListPojo.setUserId(currentUserID);
        boolean  isSysAdmin=currentUserService.CheckIsSysAdmin();
        campusListPojo.setSysAdmin(isSysAdmin);

        BaseParmDto<RolesReqDto> parmDto=new  BaseParmDto<>();
        boolean flag=false;
        Integer zeroCampus=new Integer(0);
        if(!isSysAdmin){
            Set<Integer> roleCampusIdsSet= rolesService.queryRoleCampusIdsSet(parmDto);
            campusListPojo.setRoleCampusIdsSet(roleCampusIdsSet);
            if(roleCampusIdsSet.contains(zeroCampus)){
                flag=true;
            }
            if(roleCampusIdsSet.contains(new Integer(0))){
                roleCampusIdsSet.remove(new Integer(0)) ;
            }
            if(roleCampusIdsSet.contains(null)){
                roleCampusIdsSet.remove(null) ;
            }
        }else{
            flag=true;
        }

        List<CampusResDto> campusResDtoList = departmentsMapper.queryCampusList(campusListPojo);
        if (flag){
            CampusResDto campusResDto=new CampusResDto(0,"校区名字为空");
            campusResDtoList.add(0,campusResDto);
        }
        return QueryResponse.setResData(campusResDtoList);
    }

    public boolean checkCampusId(Integer campusId) {
        Integer currentUserID = currentUserService.getUserId();
        CampusListPojo campusListPojo=new CampusListPojo();
        campusListPojo.setCampusLevelType(DepartmentLevelTypeEnum.Campus.getCode());
        campusListPojo.setUserId(currentUserID);
        boolean  isSysAdmin=currentUserService.CheckIsSysAdmin();
        campusListPojo.setSysAdmin(isSysAdmin);

        BaseParmDto<RolesReqDto> parmDto=new  BaseParmDto<>();
        boolean flag=false;
        Integer oneCampus=new Integer(0);
        if(!isSysAdmin){
            Set<Integer> roleCampusIdsSet= rolesService.queryRoleCampusIdsSet(parmDto);
            campusListPojo.setRoleCampusIdsSet(roleCampusIdsSet);
            if(roleCampusIdsSet.contains(oneCampus)){
                flag=true;
            }
            if(roleCampusIdsSet.contains(new Integer(0))){
                roleCampusIdsSet.remove(new Integer(0)) ;
            }
            if(roleCampusIdsSet.contains(null)){
                roleCampusIdsSet.remove(null) ;
            }
        }
        List<CampusResDto> campusResDtoList = departmentsMapper.queryCampusList(campusListPojo);
        if (flag){
            CampusResDto campusResDto=new CampusResDto(0,"校区名字为空");
            //campusResDto.setCampusId(0);
            //campusResDto.setCampusName("校区名字为空");
            campusResDtoList.add(campusResDto);
        }
        for (CampusResDto campusResDto:campusResDtoList){
            if(campusId.equals(campusResDto.getCampusId())){
                return true;
            }
        }
        return false;
    }
}
